#!/bin/bash

echo "🔍 火币WebSocket连接测试"
echo "========================"

# 检查是否安装了wscat
if ! command -v wscat &> /dev/null; then
    echo "❌ wscat未安装，请先安装: npm install -g wscat"
    exit 1
fi

echo "📡 连接到火币WebSocket..."
echo "地址: wss://api.huobi.pro/ws"
echo ""

# 创建临时订阅文件
cat > /tmp/huobi_subscription.json << 'EOF'
{"sub": "market.btcusdt.detail", "id": "btc_detail_test"}
EOF

echo "📤 准备发送的订阅消息:"
cat /tmp/huobi_subscription.json
echo ""
echo ""

echo "🚀 开始连接测试 (30秒后自动断开)..."
echo "预期行为:"
echo "1. 连接成功"
echo "2. 收到ping消息并自动回复pong"
echo "3. 发送订阅消息"
echo "4. 收到订阅确认"
echo "5. 收到BTC的24小时行情数据"
echo ""

# 使用wscat连接并发送订阅消息
timeout 30 wscat -c wss://api.huobi.pro/ws --ping-interval 20 << 'EOF'
{"sub": "market.btcusdt.detail", "id": "btc_detail_test"}
{"sub": "market.ethusdt.kline.1min", "id": "eth_kline_test"}
EOF

echo ""
echo "✅ 测试完成"

# 清理临时文件
rm -f /tmp/huobi_subscription.json 